package com.ss.android.auto.launch.tasks.collector.stage2

import com.bytedance.common.utility.concurrent.ThreadPlus
import com.bytedance.dataplatform.abTest.Experiments
import com.dcd.abtest.experiment.garage.OptGarageLaunchFpsV1Experiment
import com.dcd.abtest.experiment.launch.HomePageLaunchTaskOptExperiment
import com.ss.android.article.base.utils.SharedPref.SharedPrefMigrateManager
import com.ss.android.auto.account.IAccountSdkService
import com.ss.android.auto.dealersupport_api.IPrivacyInfoProtectService
import com.ss.android.auto.homepage_api.IHomepageService
import com.ss.android.auto.launch.helper.PageMappingInitHelper
import com.ss.android.auto.launch.helper.ShareModuleInitHelper
import com.ss.android.auto.launch.tasks.collector.AsyncThreadTaskCollector
import com.ss.android.auto.launch.tasks.old.WorkerBackgroundTaskA
import com.ss.android.auto.launch.tasks.old.WorkerBackgroundTaskB
import com.ss.android.auto.launch.tasks.old.WorkerBackgroundTaskNewC
import com.ss.android.auto.log.AutoLog
import com.ss.android.auto.optimize.keva.IKevaOptService
import com.ss.android.auto.optimize.serviceapi.IOptimizeService
import com.ss.android.auto.optimize.serviceapi.LaunchOptType
import com.ss.android.auto.optimize.serviceapi.OptimizeServiceKt.Companion.isLaunchOptDecemberEnable
import com.ss.android.auto.servicemanagerwrapper.AutoServiceManager
import com.ss.android.auto.thread.launch.LaunchExecutors
import com.ss.android.basicapi.ui.util.app.DimenHelper
import com.ss.android.common.app.AbsApplication
import com.ss.android.globalcard.utils.NewFeedSwitchUtil.useNewHome805
import com.ss.android.service.IAdCacheService

/**
 *  第2阶段子线程任务，并发执行。

 *  @author zhongxinyuan @ ByteDance Inc
 *  @since   2022/10/20
 */
object Stage2AsyncTasks : AsyncThreadTaskCollector() {

    override fun collectSubTasks() {
        if (HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("initAsyncTask") {
                WorkerBackgroundTaskB.initAsyncTask() // 启动不需要application等待接收的task
            }
            addSubTask("initDownloadSDK") {
                WorkerBackgroundTaskNewC.initDownloadSDK() // 初始化下载sdk
            }
            addSubTask("initUpdateModule") {
                WorkerBackgroundTaskA.initUpdateModule()
                DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), true)
                DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), false)
                IKevaOptService.get().init(AbsApplication.getApplication())
            }
        }
        addSubTask("preInitAutoCategoryManager") {
            LaunchExecutors.runHighPriorityTask {
                WorkerBackgroundTaskB.tryPreInitAutoCategoryManager()
                if (useNewHome805 && AutoServiceManager.getService2(IHomepageService::class.java) != null) {
                    AutoServiceManager.getService2(IHomepageService::class.java)?.read805CacheHeadData()
                }
            }
        }
        addSubTask("initAccountBdTruing") {
            WorkerBackgroundTaskA.initAccountBdTruing()
            ShareModuleInitHelper.init(AbsApplication.getApplication()) // 分享SDK初始化
        }
        addSubTask("hookActivityThread") {
            AutoServiceManager.getService2<IAccountSdkService>(IAccountSdkService::class.java)
                ?.init()

            WorkerBackgroundTaskA.hookActivityThread()
            if (OptGarageLaunchFpsV1Experiment.isOptEnable()) {
                SharedPrefMigrateManager.setMigrated(false)
            }
        }
        addSubTask("initSpData") {
            try {
                WorkerBackgroundTaskNewC.initRefreshSpreadInfo() // 广告相关资源初始化
                if (!HomePageLaunchTaskOptExperiment.open()) {
                    AutoServiceManager.getService2<IPrivacyInfoProtectService>(IPrivacyInfoProtectService::class.java)?.initSpData()
                }
            } catch (e: Exception) {
                AutoLog.ensureNotReachHere(e, "initRefreshSpreadInfoException")
            }
        }
        if (!HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("initDownloadSDK") {
                WorkerBackgroundTaskNewC.initDownloadSDK() // 初始化下载sdk
            }
        }
        addSubTask("initPullToFreshStyle") {

            WorkerBackgroundTaskA.initPullToFreshStyle() // 初始化下拉刷新模块

            //initApm();     需要放到主线程！！ // 初始化Slardar性能统计
            PageMappingInitHelper.init() // 注册全局页面PageId
        }
        addSubTask("initPushDialog") {
            WorkerBackgroundTaskA.initPushDialog() // 初始化PushDialog
        }
        if (!HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("initUpdateModule") {
                WorkerBackgroundTaskA.initUpdateModule()
                DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), true)
                DimenHelper.getStatusBarHeight(AbsApplication.getApplication(), false)
                IKevaOptService.get().init(AbsApplication.getApplication())
            }
        }
        if (!Experiments.getLazyCarRecognize(false)){
            addSubTask("initCarRecognize") {
                WorkerBackgroundTaskB.initCarRecognize() // 拍照识车初始化
            }
        }

        addSubTask("initGecko") {
            WorkerBackgroundTaskB.initGecko() // gecko 初始化
        }
        addSubTask("initMonitorSetting") {
            WorkerBackgroundTaskB.initMonitorSetting() // Slardar 监控相关初始化
        }
        addSubTask("preLoadJumpToConfigSchema") {
            WorkerBackgroundTaskB.preLoadJumpToConfigSchema()
        }
        if (!HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("initAsyncTask") {
                WorkerBackgroundTaskB.initAsyncTask() // 启动不需要application等待接收的task
            }
        }

        addSubTask("initByteWebView") {
            WorkerBackgroundTaskB.initByteWebView() // 初始化ByteWebView
        }
        addSubTask("initAdWebViewSDK") {
            if (isLaunchOptDecemberEnable(LaunchOptType.ASYNC)) {
                // 初始化广告落地页SDK
                ThreadPlus.submitRunnable { WorkerBackgroundTaskB.initAdWebViewSDK() }
            } else {
                WorkerBackgroundTaskB.initAdWebViewSDK() // 初始化广告落地页SDK
            }
        }
        addSubTask("initTaskManager") {
            WorkerBackgroundTaskB.initTaskManager()
        }
        if (!HomePageLaunchTaskOptExperiment.open()) {
            addSubTask("initSquareData") {
                WorkerBackgroundTaskB.initSquareData()
            }
            addSubTask("initAdCache") {
                IAdCacheService.ins().initAdUnexposedCidCache()
            }
            addSubTask("initStringBuilderOpt") {
                AutoServiceManager.getService2<IOptimizeService>(IOptimizeService::class.java)
                    ?.initStringBuilderOpt()
            }
        }
    }

}